CMAKE_MINIMUM_REQUIRED(VERSION 3.1.0 FATAL_ERROR)

SET(BRANDING_PROJECT_NAME "fastocloud" CACHE STRING "Branding for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_VERSION "1.2.0.4" CACHE STRING "Branding version for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_BUILD_TYPE_VERSION "release" CACHE STRING "Build version type for ${BRANDING_PROJECT_NAME}") #default alfa,beta,rc,release
SET(BRANDING_PROJECT_DOMAIN "www.fastogt.com" CACHE STRING "Branding domain url for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_COMPANYNAME "FastoGT" CACHE STRING "Company name for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_SUMMARY "${BRANDING_PROJECT_NAME} real-time encoding/transcoding service."
  CACHE STRING "Short description of ${BRANDING_PROJECT_NAME}")
SET(BRANDING_PROJECT_COPYRIGHT "Copyright (C) 2014-2020 ${BRANDING_PROJECT_COMPANYNAME} All Rights Reserved."
  CACHE STRING "Copyright notice for ${BRANDING_PROJECT_NAME}") #default
SET(BRANDING_PROJECT_CHANGELOG_FILE CHANGELOG
  CACHE STRING "Branding for changelog file ${BRANDING_PROJECT_NAME}
  (File name given as relative paths are interpreted with respect to the src source directory)") #default
SET(BRANDING_PROJECT_LICENSE_FILE_NAME LICENSE
  CACHE STRING "Branding for license file ${BRANDING_PROJECT_NAME}
  (File name given as relative paths are interpreted with respect to the src source directory)") #default

PROJECT(${BRANDING_PROJECT_NAME} VERSION ${BRANDING_PROJECT_VERSION} LANGUAGES CXX C)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(CMAKE_C_STANDARD 99)
SET(CMAKE_C_STANDARD_REQUIRED ON)
#SET(CMAKE_C_EXTENSIONS OFF)
#################### Project Settings ####################
SET(PROJECT_NAME_TITLE ${PROJECT_NAME}) #PROJECT_NAME in cache
SET(PROJECT_DOMAIN ${BRANDING_PROJECT_DOMAIN})
SET(PROJECT_COMPANYNAME ${BRANDING_PROJECT_COMPANYNAME})
SET(PROJECT_SUMMARY ${BRANDING_PROJECT_SUMMARY})
SET(PROJECT_COPYRIGHT ${BRANDING_PROJECT_COPYRIGHT})
SET(PROJECT_CHANGELOG_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${BRANDING_PROJECT_LICENSE_FILE_NAME})
SET(PROJECT_LICENSE_FILE_NAME ${BRANDING_PROJECT_LICENSE_FILE_NAME})

SET(DATE_CMD "date")
SET(DATE_ARGS "+%a %b %d %Y")
EXECUTE_PROCESS(COMMAND ${DATE_CMD} ${DATE_ARGS} RESULT_VARIABLE CHANGELOG_TIMESTAMP_RESULT OUTPUT_VARIABLE CHANGELOG_TIMESTAMP)  #for rpm package
IF (NOT "${CHANGELOG_TIMESTAMP}" STREQUAL "")
 STRING(REPLACE "\n" "" CHANGELOG_TIMESTAMP ${CHANGELOG_TIMESTAMP})
ELSE()
 MESSAGE(WARNING "Failed to get timestamp: ${CHANGELOG_TIMESTAMP_RESULT}")
ENDIF(NOT "${CHANGELOG_TIMESTAMP}" STREQUAL "")
FILE(WRITE ${PROJECT_CHANGELOG_FILE} "* ${CHANGELOG_TIMESTAMP} ${PROJECT_MAINTAINER_NAME} <${PROJECT_MAINTAINER_MAIL}>\n")
FILE(READ ${BRANDING_PROJECT_CHANGELOG_FILE} CHANGELOG_TEXT)
FILE(APPEND ${PROJECT_CHANGELOG_FILE} ${CHANGELOG_TEXT})

SET(PROJECT_BUILD_TYPE_VERSION ${BRANDING_PROJECT_BUILD_TYPE_VERSION})
##########################################################
STRING(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWERCASE)
STRING(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPERRCASE)

SET(PROJECT_VERSION_SHORT ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}) #PROJECT_VERSION_* in cache
SET(PROJECT_VERSION_INTEGER ${PROJECT_VERSION_MAJOR}${PROJECT_VERSION_MINOR}${PROJECT_VERSION_PATCH}) #PROJECT_VERSION_* in cache

MESSAGE(STATUS "PROJECT_VERSION: ${PROJECT_VERSION}")

OPTION(DEVELOPER_ENABLE_TESTS "Enable tests for ${PROJECT_NAME_TITLE} project" OFF)
OPTION(DEVELOPER_CHECK_STYLE "Enable check style for ${PROJECT_NAME_TITLE} project" OFF)
OPTION(DEVELOPER_GENERATE_DOCS "Generate docs api for ${PROJECT_NAME_TITLE} project" OFF)
OPTION(BUILD_SERVER "Build server for ${PROJECT_NAME_TITLE} project" ON)
OPTION(BUILD_STREAM "Build stream for ${PROJECT_NAME_TITLE} project" ON)
OPTION(CPACK_SUPPORT "Enable package build" OFF)

##################################DEFAULT VALUES##########################################
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE DEBUG)
ENDIF(NOT CMAKE_BUILD_TYPE)

# If the user did not customize the install prefix,
# set it to live under build so we don't inadvertently pollute /usr/local
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "default install path" FORCE)
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

IF("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")
  MESSAGE(SEND_ERROR "In-source builds are not allowed.")
ENDIF("${PROJECT_SOURCE_DIR}" STREQUAL "${PROJECT_BINARY_DIR}")

MESSAGE(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

############################################################################

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
INCLUDE(config) ###################
DEFINE_DEFAULT_DEFINITIONS(ON, ON, OFF)
DEFINE_PROJECT_DEFINITIONS()

SET(PROJECT_VERSION_HUMAN "${PROJECT_VERSION} Revision: ${PROJECT_VERSION_GIT}")

IF(OS_WINDOWS)
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
ELSEIF(OS_MACOSX)
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
ELSEIF(OS_LINUX)
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
ELSEIF(OS_FREEBSD)
  SET(LIB_INSTALL_DESTINATION lib)
  SET(TARGET_INSTALL_DESTINATION bin)
  SET(SHARE_INSTALL_DESTINATION share)
ELSEIF(OS_ANDROID)
  SET(LIB_INSTALL_DESTINATION libs/${ANDROID_NDK_ABI_NAME})
  SET(TARGET_INSTALL_DESTINATION libs/${ANDROID_NDK_ABI_NAME}) #libs/armeabi-v7a
  SET(SHARE_INSTALL_DESTINATION share)
ENDIF(OS_WINDOWS)

IF(PROJECT_BUILD_TYPE_VERSION STREQUAL "release")
  ADD_DEFINITIONS(-DPROJECT_BUILD_RELEASE)
ENDIF(PROJECT_BUILD_TYPE_VERSION STREQUAL "release")

ADD_DEFINITIONS(-DPROJECT_VERSION_HUMAN="${PROJECT_VERSION_HUMAN}")

SET(STREAMER_SERVICE_NAME ${PROJECT_NAME_LOWERCASE})

ADD_SUBDIRECTORY(src)

IF(DEVELOPER_GENERATE_DOCS)
  CREATE_DOCS(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
ENDIF(DEVELOPER_GENERATE_DOCS)

IF(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
  EXECUTE_PROCESS(COMMAND ${CMAKE_SOURCE_DIR}/scripts/create_pre_commit_hook.sh)
ENDIF(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
